package cn.lyl.org.service.impl;

import cn.lyl.basic.service.impl.BaseServiceImpl;
import cn.lyl.basic.util.Md5Utils;
import cn.lyl.basic.util.StrUtils;
import cn.lyl.org.domain.Employee;
import cn.lyl.org.domain.Employee;
import cn.lyl.org.mapper.EmployeeMapper;
import cn.lyl.org.service.IEmployeeService;
import cn.lyl.user.domain.Logininfo;
import cn.lyl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：部门
 */
@Service//1.将当前类交给Spring管理 2.实例化对象注解 3.创建对象的疏解；前提：扫描包路径s
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用

public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 添加
     * @param employee
     */
    @Transactional
    @Override
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //【登录信息】创建Logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//0-管理员
        logininfo.setDisable(true);
        //将Logininfo对象添加到数据库，并生成自增id
        logininfoMapper.add(logininfo);//自增长id

        //【员工】将logininfo_id设置到employee对象中
        employee.setLogininfo_id(logininfo.getId());
        //添加到数据库
        super.add(employee);
        //【角色】为当前员工添加角色信息
        if(employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }

    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        //根据id找到员工 从而得知他的Logininfo_id
        Employee employee = employeeMapper.findOne(id);
        //那么logininfo就可以知道自己的id 删除自己
        logininfoMapper.del(employee.getLogininfo_id());

        employeeMapper.delEmployeeRoleByEmployeeId(id);
        super.del(id);
    }
    @Transactional
    @Override
    public void update(Employee employee) {
        //盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String Md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(Md5Pwd);
        //前端修改数据，并不会修改logininfoid  shopid  需要这里得到
        Employee oldEmployee = employeeMapper.findOne(employee.getId());

        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);
        //logininfo根据旧数据找到旧员工的logininfo_id找到之前的自己
        System.out.println(oldEmployee);
        Logininfo oldLogininfo = logininfoMapper.findOne(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //【维护角色t_employee_role数据】修改当前员工的角色
        if(employee.getRole_id()!=null){
            //删除角色信息
            employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
            //再添加新的角色信息
            employeeMapper.addRole(employee);
        }

    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {

        return employeeMapper.queryByShopId(shopId);
    }
}
